(function(ns) {
    ZYCLib.RegNameSpace(ns).HeartBeatPackage = (function() {
        var options = {
            frequency: 30,
            url: "Common/UserSignService/HeartBeat"
        };
        var key = "__reqtime";
        var executeingAjax = false;
        var timer;
        var clearTimer = function() {
            if (timer) {
                clearTimeout(timer);
                timer = null;
            }
        };

        var isTriggerPoint = function() {
            var now = new Date();
            var dt = now;
            var dts = ZYCLib.Utils.LocalStorage.Get(key) * 1;
            if (dts && dts > 1) dt = new Date(dts);
            if (now.setMinutes(now.getMinutes() + 3) < dt) dt = now;
            if (now >= dt) {
                return true;
            }
            return false;
        };

        var exeRequest = function() {
            if (executeingAjax) {
                return;
            }
            var curTime = new Date();
            var now = curTime.setMinutes(curTime.getMinutes() + 2);
            console.log("心跳包下次执行：" + now);
            ZYCLib.Utils.LocalStorage.Set(key, now);
            executeingAjax = true;
            ZYCLib.Web.JsonAjax.PostData(options.url,
                {
                    Title: document.title,
                    Href: document.location.href
                }, function() {},
                function() {
                    executeingAjax = false;
                },
                {
                    Error5: function () { clearTimer(); },
                    Error1: function () { }
                });
        };

        var onTimerEvent = function() {
            if (isTriggerPoint()) {
                exeRequest();
            }
            timer = setTimeout(onTimerEvent, options.frequency * 1000);
        };

        return {
            Start: function() {
                if (ZYCLib.CurrentUser.PerID < 1) return false;
                onTimerEvent();
                return true;
            }
        };
    })();
})("PlugIn");